package web;

import utils.Constant;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;

@WebServlet("/offline")
public class OfflineServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        ServletContext app = req.getServletContext();
        ConcurrentHashMap<String, HttpSession> map = (ConcurrentHashMap<String, HttpSession>)
                app.getAttribute(Constant.ONLINE_USER);
        HttpSession remove = map.remove(id);
        if (remove != null) {
            // 让session过期
            remove.invalidate();
        }
        resp.sendRedirect("/login");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
